package com.gla.controller;

import java.io.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.gla.commom.Result;
import com.gla.dao.OrderMapper;
import com.gla.dao.ShopMapper;
import com.gla.dto.OrderDetailDTO;
import com.gla.dto.OrderInfoDTO;
import com.gla.entity.Index;
import com.gla.entity.Shop;
import com.gla.service.FYHService;
import com.gla.service.IndexService;
import com.gla.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/index")
public class IndexController {
    @Autowired
    IndexService indexService;
    @Autowired
    OrderService orderService;
    @Autowired
    FYHService fyhService;

    @Resource
    OrderMapper orderMapper;

    @GetMapping(value = "/getpoints")
    public Result getPoints(){
        List<Shop> list = fyhService.selectAllShop();
        PageInfo pageInfo = new PageInfo(list);
        return Result.succeed(pageInfo);
    }

    @GetMapping(value = "/getsales")
    public Result getsales(){
//        List<Index> list = indexService.selectPriceAndId();
//        List<Index> list1 = indexService.selectNumberAndId();
        List<Index> timeList = indexService.selecTimeAndId();
        List<Integer> orderIdList = indexService.selectOrder_id();

//        List<Double> sales = new ArrayList<>();
//        for (Integer i = 0; i< list.size();i++){
//            sales.add(list.get(i).getPrice()*list1.get(i).getNumber());
//
//        }
        Double[] result = new Double[12];
        for(int i = 0;i<12;i++){
            result[i] = 0.0;
        }
        for(int i=0;i<timeList.size();i++){
            double total = 0;
            List<OrderDetailDTO> commodityList = orderMapper.getOrderDetails(timeList.get(i).getId());
            for(OrderDetailDTO item : commodityList){
                //计算钱
                total += item.getNumber() * item.getCommodity().getPrice();
            }
            result[Integer.parseInt(timeList.get(i).getCreate_time())-1] += total;
        }
        return Result.succeed(result);
    }

    @GetMapping(value = "/gettoday")
    public Result getToday(){
//        List<Index> list = indexService.selectPriceAndId();
//        List<Index> list1 = indexService.selectNumberAndId();
        List<Index> timeList = indexService.selectToday();

//        List<Double> sales = new ArrayList<>();
//        for (Integer i = 0; i< list.size();i++){
//            sales.add(list.get(i).getPrice()*list1.get(i).getNumber());
//
//        }
        double total = 0;
        for(int i=0;i<timeList.size();i++){

            List<OrderDetailDTO> commodityList = orderMapper.getOrderDetails(timeList.get(i).getId());
            for(OrderDetailDTO item : commodityList){
                //计算钱
                total += item.getNumber() * item.getCommodity().getPrice();
            }
        }
        return Result.succeed(total);
    }

}
